package spil;

import desktop_resources.GUI;

public class Territory extends Ownable {
	private int rent;
	private int startRent;
	private int price;
	private int housePrice;
	private int hotelPrice;
	protected boolean[] houses;

	public Territory(int rent, int price, int housePrice, int hotelPrice, int fieldNumber){
		super(price, fieldNumber);
		startRent = rent;
		this.rent = rent;
		this.price = price;
		this.housePrice = housePrice;
		this.hotelPrice = hotelPrice;
		houses = new boolean[5];
	}

	public void landOnField(Player p) {
		//		buy = false;
		if(owner == null) {
			boolean buy = GUI.getUserLeftButtonPressed(title + " er ikke ejet", "køb", "ignorer");
			if(buy) {
				buy(p);
				rent = startRent;
			}
			else GUI.getUserButtonPressed(p + " købte ikke " + title, "ok");
		}
		else {
			if(p!=owner) {
				if(p.getAccount().getBalance() < rent) {
					GUI.getUserButtonPressed(p + " betaler " + p.getBalance() + "\ntil " + owner, "ok");
					owner.getAccount().insert(p.getBalance());
					p.getAccount().withdraw(p.getBalance());
				}
				else {
					GUI.getUserButtonPressed(p + " betaler " + rent + "\ntil " + owner, "ok");
					p.getAccount().withdraw(rent);
					owner.getAccount().insert(rent);
				}
			}
			else {
				GUI.getUserButtonPressed(owner + " ejer " + title, "ok");
				//
				if((p.getBalance() > housePrice) && !houses[3] && !houses[4]) {
					boolean buildHouse = GUI.getUserLeftButtonPressed("Byg et hus? ", "byg", "nej");
					if(buildHouse) {
						buildHouse();
						GUI.getUserButtonPressed(p + " byggede et hus", "ok");
					}
					else {
						GUI.getUserButtonPressed(p + " byggede ikke noget hus", "ok");
					}
				}
				else if((p.getBalance() > hotelPrice) && !houses[4]) {
					boolean buildHotel = GUI.getUserLeftButtonPressed("Byg et hotel? ", "byg", "nej");
					if(buildHotel) {
						buildHotel();
						GUI.getUserButtonPressed(p + " byggede et hotel", "ok");
					}
					else {
						GUI.getUserButtonPressed(p + " byggede ikke noget hotel", "ok");
					}
				}
			}
		}
	}




	public int getRent(){
		return rent;
	}

	public void buildHouse(){
		int i = 0;
		while(houses[i]) {
			i++;
		}
		houses[i] = true;
		owner.getAccount().withdraw(housePrice);
		rent += housePrice/10;
		GUI.setHouses(fieldNumber, i+1);
	}

	public void buildHotel(){
		owner.getAccount().withdraw(hotelPrice);
		rent = startRent + housePrice;
		houses[4] = true;
		GUI.setHotel(fieldNumber, houses[4]);
	}

	public void lose(Player p) {
		if(owner == p) {
			owner = null;
			GUI.setOwner(fieldNumber, p.getName());
			GUI.removeOwner(fieldNumber);
			GUI.setHotel(fieldNumber, false);
			GUI.setHouses(fieldNumber, 0);
		for(int i = 0; i < houses.length; i++)
			houses[i] = false;
		}
	}
}
